package shop;

import java.util.Scanner;

public class GoodDemo {
    public static void main(String[] args) {
        Good good = new Good();
        Good[] shopCar = new Good[100];
        while (true) {
            System.out.println("请选则您要执行的操作：");
            System.out.println("添加商品：add");
            System.out.println("查看商品：query");
            System.out.println("修改商品：update");
            System.out.println("结算商品：pay");
            Scanner sc = new Scanner(System.in);
            System.out.println("请选择：");
            String choice = sc.next();
            switch(choice) {
                case"add":
                    addGood(shopCar,sc);
                    break;
                case"query":
                    queryGood(shopCar);
                    break;
                case"update":
                    updateGood(shopCar,sc);
                    break;
                case"pay":
                    payGood(shopCar);
                    break;
                default:
                    System.out.println("您输入的信息有误！！");
            }
        }
    }

    public static void payGood(Good[] shopCar) {
        System.out.println("您购买的商品是：");
        queryGood(shopCar);
        double sum =0;
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i]!=null) {
                sum += shopCar[i].number * shopCar[i].price;
            }
        }
        System.out.println("您共需要支付"+sum);
    }

    public static void updateGood(Good[] shopCar,Scanner sc) {
        System.out.println("请输入您要修改的商品编号：");
        int n = sc.nextInt();
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i].id==n) {
                System.out.println("请输入修改后购买的商品数量：");
                int number2 =sc.nextInt();
                shopCar[i].number=number2;
                System.out.println("修改成功啦！！！");
            } else {
                System.out.println("您未添加该商品");
            }
        }

    }

    public static void queryGood(Good[] shopCar) {
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i]!=null) {
                System.out.println(shopCar[i]);
            } else {
                break;
            }
        }
    }

    public static void addGood(Good[] shopCar,Scanner sc) {
            Good g = new Good();
        System.out.println("请输入商品的编号：");
        int id = sc.nextInt();
        System.out.println("请输入商品的名称：");
        String name = sc.next();
        System.out.println("请输入商品的价格：");
        double price = sc.nextDouble();
        System.out.println("请输入要购买的数量：");
        int number =sc.nextInt();
        g.id=id;
        g.name=name;
        g.price=price;
        g.number=number;
        for (int i = 0; i < shopCar.length; i++) {
            if(shopCar[i]==null) {
                shopCar[i]=g;
                break;
            }
        }
        System.out.println("添加成功");
    }
}
